package com.bdsy.business.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bdsy.business.dao.ShopImageMapper;
import com.bdsy.business.model.ShopImage;
import com.bdsy.business.service.ShopImageService;
import com.bdsy.common.utils.DateRandomUtil;
import com.bdsy.common.utils.ImageUploadUtils;
import com.bdsy.common.utils.ZipUtil;

@Service("shopImageService")
public class ShopImageServiceImpl implements ShopImageService {

	@Autowired
	ShopImageMapper shopImageMapper;
	
	@Override
	public boolean add(ShopImage shopImage,MultipartFile file,HttpServletRequest request) {
		//获取图片路径
		//服务器项目目录路径
//		String path = request.getSession().getServletContext().getRealPath("/images"); 
//		String path = "E:\\Program Files\\Apache Software Foundation\\apache-tomcat-8.5.23\\webapps\\adicms_image\\images";
		String path = "/usr/local/tomcat/webapps/adicms_image/images";
		//文件名
//				String fileName = file.getOriginalFilename();
		int result=0;
		if(file !=null){
			//文件类型
			String contentType = file.getContentType();
			//获得文件后缀名   
	        String suffixName=contentType.substring(contentType.indexOf("/")+1); 
	        //生成uuid作为文件名称    
	        String uuid = UUID.randomUUID().toString().replaceAll("-","");    
	        //得到 文件名  
	        String fileName=uuid+"."+suffixName;  
	        
	        System.out.println(path+"--***---"+contentType+"--***---"+suffixName+"---***---"+fileName);
			//生成文件
		    File dir = new File(path, fileName);  
	        if(!dir.exists()){  
	            dir.mkdirs();  
	        }   
	        System.out.println(dir.getAbsolutePath()+"///////");
	        try {
				file.transferTo(dir);
			}catch (IOException e) {
				System.out.println("文件转换失败");
				e.printStackTrace();
			}
	        
	        //解压缩上传的照片
	        File upload = new File(path);//解压文件存储的路径
			List<File> list = ZipUtil.upzipFile(dir, upload.getAbsolutePath());
			//删除压缩文件
			ZipUtil.deletefile(dir.toString());
			System.out.println(list+"///////-----");
			
			String imageUrl = "";
			for (File file2 : list) {
				shopImage.setImageUrl(file2.toString());
				imageUrl += file2.toString()+";";
			}
			shopImage.setImageUrl(imageUrl);
			result = shopImageMapper.insert(shopImage);

		}
		return result>0?true:false;
	}

	@Override
	public boolean update(ShopImage shopImage) {
		int result = shopImageMapper.update(shopImage);
		return result>0?true:false;
	}

	@Override
	public boolean addImage(ShopImage shopImage, MultipartFile file) {
		//图片上传
		//服务器项目目录路径
//		String path = "E:\\Program Files\\apache-tomcat-8.5.23\\webapps\\adicms_image\\images";
		String path = "/usr/local/tomcat/webapps/adicms_image/images";
		if(file !=null){
			//文件类型
			String contentType = file.getContentType();
			//获得文件后缀名   
	        String suffixName=contentType.substring(contentType.indexOf("/")+1); 
	        //生成uuid作为文件名称    
//			        String uuid = UUID.randomUUID().toString().replaceAll("-","");    
	        //生成当前日期加五位随机数作为文件名称    
            String cfileName = DateRandomUtil.getRandomFileName();  
	        //得到 文件名  
	        String fileName=cfileName+"."+suffixName;  
	        
	        System.out.println(path+"--***---"+contentType+"--***---"+suffixName+"---***---"+fileName);
			//生成文件
		    File dir = new File(path, fileName);  
	        if(!dir.exists()){  
	            dir.mkdirs();  
	        }   
	        System.out.println(dir.getAbsolutePath()+"///////");
	        try {
				file.transferTo(dir);
			}catch (IOException e) {
				System.out.println("文件转换失败");
				e.printStackTrace();
			}
	        
	        File upload = new File(path);//解压文件存储的路径
			List<File> list = ZipUtil.upzipFile(dir, upload.getAbsolutePath());
			//删除压缩文件
			ZipUtil.deletefile(dir.toString());
			System.out.println(list+"///////-----");
			
			String url = "";
			for (File file2 : list) {
				url += file2.toString()+";";
			}
	        
	        //把图片的相对路径保存至数据库
//	       String url= "/images/" + fileName;
	       shopImage.setImageUrl(url);
		}
	       int 	result = shopImageMapper.updateImages(shopImage);
	       
	       return result>0?true:false;
	}

	@Override
	public List<ShopImage> shopImageById(int shopId) {
		List<ShopImage> shopImage = shopImageMapper.shopImageById(shopId);
		return shopImage;
	}

	@Override
	public boolean updateImage(ShopImage shopImage, MultipartFile file) {
		//图片上传
		//服务器项目目录路径
//		String path = "E:\\Program Files\\Apache Software Foundation\\apache-tomcat-8.5.23\\webapps\\adicms_image\\imges";
//		String path = "C:\\Program Files\\apache-tomcat-8.5.23\\webapps\\adicms_image\\imges";
		String path = "/usr/local/tomcat/webapps/adicms_image/imges";
		if(file !=null){
			//文件类型
			String contentType = file.getContentType();
			//获得文件后缀名   
	        String suffixName=contentType.substring(contentType.indexOf("/")+1); 
	        //生成uuid作为文件名称    
//					        String uuid = UUID.randomUUID().toString().replaceAll("-","");    
	        //生成当前日期加五位随机数作为文件名称    
            String cfileName = DateRandomUtil.getRandomFileName();  
	        //得到 文件名  
	        String fileName=cfileName+"."+suffixName;  
	        
	        System.out.println(path+"--***---"+contentType+"--***---"+suffixName+"---***---"+fileName);
			//生成文件
		    File dir = new File(path, fileName);  
	        if(!dir.exists()){  
	            dir.mkdirs();  
	        }   
	        System.out.println(dir.getAbsolutePath()+"///////");
	        try {
				file.transferTo(dir);
			}catch (IOException e) {
				System.out.println("文件转换失败");
				e.printStackTrace();
			}
	        
	        
	   //  -----------------------------------------------------   
	        //解压缩上传的照片
	        File upload = new File(path);//解压文件存储的路径
			List<File> list = ZipUtil.upzipFileg(dir, upload.getAbsolutePath());
			//删除压缩文件
			ZipUtil.deletefile(dir.toString());
			System.out.println(list+"///////-----");
			
			String url = "";
			for (File file2 : list) {
				url += file2.toString()+";";
			}
	    //--------------------------------------------------------    
	        
	        //把图片的相对路径保存至数据库
	   //    String url= "/imges/" + fileName;
	       
	       //判断数据库中是否存在该图片信息 如果存在 拼接修改
	       // 根据ID 查询 采集图片 
			List<ShopImage> shopImag = shopImageMapper.shopImageById(shopImage.getShopId());
			String cImageUrl = shopImag.get(0).getcImageUrl();
			if(cImageUrl ==null || "".equals(cImageUrl)){
				shopImage.setcImageUrl(url);
			}else{
				shopImage.setcImageUrl(cImageUrl+";"+url);
			}
		}
	       int 	result = shopImageMapper.updateImg(shopImage);
	       return result>0?true:false;
	}

	@Override
	public boolean insert(ShopImage shopImage) {
		  int result = shopImageMapper.insert(shopImage);
		  return result>0?true:false;
	}

}
